package com.nhjf.mobile.controller.coursesPlayBack;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.nhjf.common.page.Pagination;
import com.nhjf.mobile.common.util.SysProperties;
import com.nhjf.model.coursesPlayback.CoursesPlayback;
import com.nhjf.model.student.Student;
import com.nhjf.model.system.AdminSession;
import com.nhjf.model.system.Role;
import com.nhjf.model.system.User;
import com.nhjf.model.teachers.Courses;
import com.nhjf.model.teachers.CoursesCollection;
import com.nhjf.model.teachers.OrganizationHasStudent;
import com.nhjf.model.teachers.RecentlyStudied;
import com.nhjf.model.teachers.Teacher;
import com.nhjf.pojo.qo.coursesPlayback.CoursesPlaybackQo;
import com.nhjf.pojo.qo.studnet.StudentQo;
import com.nhjf.pojo.qo.system.AdminSessionQo;
import com.nhjf.pojo.qo.system.UserQo;
import com.nhjf.pojo.qo.teachers.CoursesCollectionQo;
import com.nhjf.pojo.qo.teachers.CoursesCommentQo;
import com.nhjf.pojo.qo.teachers.CoursesQo;
import com.nhjf.pojo.qo.teachers.OrganizationHasStudentQo;
import com.nhjf.pojo.qo.teachers.RecentlyStudiedQo;
import com.nhjf.pojo.qo.teachers.TeachersQo;
import com.nhjf.service.coursesPlayback.CoursesPlaybackService;
import com.nhjf.service.student.StudentService;
import com.nhjf.service.system.AdminSessionService;
import com.nhjf.service.system.UserService;
import com.nhjf.service.teachers.CoursesCollectionService;
import com.nhjf.service.teachers.CoursesCommentService;
import com.nhjf.service.teachers.CoursesService;
import com.nhjf.service.teachers.OrganizationHasStudentService;
import com.nhjf.service.teachers.RecentlyStudiedService;
import com.nhjf.service.teachers.TeachersService;

import net.sf.json.JSONObject;

/**
 * @类功能说明：视频回放管理
 * @部门：技术部
 * @作者：龚一凡
 * @版本：V1.0
 */
@Controller
@RequestMapping(value = "/coursesPlayBack")
public class CoursesPlayBackController {
	@Autowired
	private CoursesPlaybackService coursesPlaybackService;
	@Autowired
	private CoursesService coursesService;
	@Autowired
	private UserService userService;
	@Autowired
	private TeachersService teacherService;
	@Autowired
	private CoursesCommentService coursesCommentService;
	@Autowired
	private CoursesCollectionService coursesCollectionService;
	@Autowired
	private AdminSessionService adminSessionService;
	@Autowired
	private RecentlyStudiedService  recentlyStudiedService;
	@Autowired
	private StudentService studentService;
	@Autowired
	private OrganizationHasStudentService osdService; 
	//课程回放列表
	@ResponseBody
	@RequestMapping(value = "/coursesPlayBackpage")
	public String coursesPlayBackpage(@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo qo= new StudentQo();
					qo.setSeachStudent(true);
					qo.setUserid(user.getId());
					Student student=studentService.queryUnique(qo);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesPlaybackQo coursesPlaybackQo=new CoursesPlaybackQo();
						coursesPlaybackQo.setSeachTeacher(true);
						coursesPlaybackQo.setCourseShares(1);
						coursesPlaybackQo.setOrganizationId(hasStudent.getOrganization().getId());//所属机构id
						pagination.setCondition(coursesPlaybackQo);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							TeachersQo teachersQo=new TeachersQo();
							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
							teachersQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teachersQo);
							coursesPlayback.setUserId(teacher.getUser().getId());
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", coursesPlaybackList);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo qo=new TeachersQo();
					qo.setUserId(user.getId());
					qo.setSeachOrganization(true);
					qo.setSeachUser(true);
					Teacher teacher1=teacherService.queryUnique(qo);
					if(teacher1!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesPlaybackQo coursesPlaybackQo=new CoursesPlaybackQo();
						coursesPlaybackQo.setSeachTeacher(true);
						coursesPlaybackQo.setCourseShares(1);
						coursesPlaybackQo.setOrganizationId(teacher1.getTraininginstitution().getId());//所属机构id
						pagination.setCondition(coursesPlaybackQo);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							TeachersQo teachersQo=new TeachersQo();
							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
							teachersQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teachersQo);
							coursesPlayback.setUserId(teacher.getUser().getId());
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", coursesPlaybackList);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
				
			}

		}

	}
	/**
	 * 课程回放列表
	 * @param teacherId
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesPlayBackTeacherpage")
	public String coursesPlayBackTeacherpage(@RequestParam(value = "page", defaultValue = "1")Integer page,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo qo1= new StudentQo();
					qo1.setSeachStudent(true);
					qo1.setUserid(user.getId());
					Student student=studentService.queryUnique(qo1);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setOrganizationId(hasStudent.getOrganization().getId());
//						qo.setUploader(Long.parseLong(teacherId));
						qo.setSeachTeacher(true);
						pagination.setCondition(qo);

						pagination.setPageNo(page);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackLisr=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackLisr) {
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo tqo=new TeachersQo();
					tqo.setSeachOrganization(true);
					tqo.setSeachUser(true);
					tqo.setUserId(user.getId());
					Teacher teach=teacherService.queryUnique(tqo);
					if(teach!=null) {
						Pagination pagination = new Pagination();
						pagination.setPageSize(8);
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setOrganizationId(teach.getTraininginstitution().getId());
//						qo.setUploader(Long.parseLong(teacherId));
						qo.setSeachTeacher(true);
						pagination.setCondition(qo);

						pagination.setPageNo(page);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackLisr=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackLisr) {
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
				
				
			}

		}

	}
	/**
	 * 课程回放详情
	 * @param coursesPlaybackId
	 * @param token
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesPlayBackDetails")
	public String coursesPlayBackDetails( @RequestParam(value="coursesPlaybackId", required=true) String coursesPlaybackId,@RequestParam(value="token", required=true) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo1=new AdminSessionQo();
		adminSessionQo1.setToken(token);
		AdminSession adminSession1=adminSessionService.queryUnique(adminSessionQo1);
		if(adminSession1==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession1.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo stuqo= new StudentQo();
					stuqo.setUserid(user.getId());
					stuqo.setSeachStudent(true);
					Student student=studentService.queryUnique(stuqo);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCoursesPlaybackId(Long.parseLong(coursesPlaybackId));
						qo.setOrganizationId(hasStudent.getOrganization().getId());
						qo.setSeachTeacher(true);
						CoursesPlayback coursesPlayback=coursesPlaybackService.queryUnique(qo);
						if(coursesPlayback!=null) {
							TeachersQo teacherQo=new TeachersQo();
							teacherQo.setTeacherId(coursesPlayback.getUploader().getId());
							teacherQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teacherQo);
							coursesPlayback.setSelfintroduction(teacher.getUser().getSelfintroduction());
							coursesPlayback.setRqz((coursesPlayback.getRqz()==null?0:coursesPlayback.getRqz())+1);
							coursesPlaybackService.update(coursesPlayback);
							CoursesCommentQo coursesCommentQo=new CoursesCommentQo();
							coursesCommentQo.setCoursesId(Long.parseLong(coursesPlaybackId));
							Integer coursesCommentCount=coursesCommentService.queryCount(coursesCommentQo);
							CoursesCollectionQo coursesCollectionQo=new CoursesCollectionQo();
							coursesCollectionQo.setCoursesId(Long.parseLong(coursesPlaybackId));
							Integer coursesCollectionCount=coursesCollectionService.queryCount(coursesCollectionQo);
							coursesPlayback.setCoursesCommentCount(coursesCommentCount);
							coursesPlayback.setCoursesCollectionCount(coursesCollectionCount);
							coursesPlayback.setUserId(teacher.getUser().getId());

							if(StringUtils.isNotEmpty(token)) {
								if(user!=null) {
									CoursesCollectionQo coursesCollectionQo1=new CoursesCollectionQo();
									coursesCollectionQo1.setUserId(user.getId());
									coursesCollectionQo1.setCoursesId(Long.parseLong(coursesPlaybackId));
									CoursesCollection collection1=coursesCollectionService.queryUnique(coursesCollectionQo1);
									if(collection1!=null) {
										coursesPlayback.setIsCollection(1);
									}
									RecentlyStudiedQo recentlyStudiedQo=new RecentlyStudiedQo();
									recentlyStudiedQo.setSeachCourses(true);
									recentlyStudiedQo.setSeachTeacher(true);
									recentlyStudiedQo.setSeachUser(true);
									recentlyStudiedQo.setCoursesId(Long.parseLong(coursesPlaybackId));
									recentlyStudiedQo.setUserId(user.getId());
									RecentlyStudied recentlyStudied1= recentlyStudiedService.queryUnique(recentlyStudiedQo);
									if(recentlyStudied1!=null) {
										recentlyStudied1.setRecentLearnTime(new Date());
										recentlyStudiedService.update(recentlyStudied1);
									}else {
										//最近观看
										//										RecentlyStudied recentlyStudied=new RecentlyStudied();
										//										recentlyStudied.setTeacher(courses.getUploader());
										//										recentlyStudied.setUser(user);
										//										recentlyStudied.setRecentLearnTime(new Date());
										//										recentlyStudied.setCourses(courses);
										//										recentlyStudiedService.save(recentlyStudied);
									}

								}
							}
						}

						if(coursesPlayback.getIsPay()!=null&&coursesPlayback.getIsPay()==1) {
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							if(coursesPlayback.getUploader()!=null&&coursesPlayback.getUploader().getAvatar()!=null){
								coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
							}
							resMap.put("code", "200");


							coursesPlayback.setVideo(imagePath+"/videofile/"+coursesPlayback.getVideo());
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							resMap.put("message", coursesPlayback);
							return JSONObject.fromObject(resMap).toString();
						}else {
							resMap.put("code", "200");
							coursesPlayback.setVideo("");
							resMap.put("message", coursesPlayback);
							return JSONObject.fromObject(resMap).toString();
						}
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo teahcQo=new TeachersQo();
					teahcQo.setSeachOrganization(true);
					teahcQo.setSeachUser(true);
					teahcQo.setUserId(user.getId());
					Teacher teachers=teacherService.queryUnique(teahcQo);
					if(teachers!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCoursesPlaybackId(Long.parseLong(coursesPlaybackId));
						qo.setOrganizationId(teachers.getTraininginstitution().getId());
						qo.setSeachTeacher(true);
						CoursesPlayback coursesPlayback=coursesPlaybackService.queryUnique(qo);
						if(coursesPlayback!=null) {
							TeachersQo teacherQo=new TeachersQo();
							teacherQo.setTeacherId(coursesPlayback.getUploader().getId());
							teacherQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teacherQo);
							coursesPlayback.setSelfintroduction(teacher.getUser().getSelfintroduction());
							coursesPlayback.setRqz((coursesPlayback.getRqz()==null?0:coursesPlayback.getRqz())+1);
							coursesPlaybackService.update(coursesPlayback);
							CoursesCommentQo coursesCommentQo=new CoursesCommentQo();
							coursesCommentQo.setCoursesId(Long.parseLong(coursesPlaybackId));
							Integer coursesCommentCount=coursesCommentService.queryCount(coursesCommentQo);
							CoursesCollectionQo coursesCollectionQo=new CoursesCollectionQo();
							coursesCollectionQo.setCoursesId(Long.parseLong(coursesPlaybackId));
							Integer coursesCollectionCount=coursesCollectionService.queryCount(coursesCollectionQo);
							coursesPlayback.setCoursesCommentCount(coursesCommentCount);
							coursesPlayback.setCoursesCollectionCount(coursesCollectionCount);
							coursesPlayback.setUserId(teacher.getUser().getId());

							if(StringUtils.isNotEmpty(token)) {
								if(user!=null) {
									CoursesCollectionQo coursesCollectionQo1=new CoursesCollectionQo();
									coursesCollectionQo1.setUserId(user.getId());
									coursesCollectionQo1.setCoursesId(Long.parseLong(coursesPlaybackId));
									CoursesCollection collection1=coursesCollectionService.queryUnique(coursesCollectionQo1);
									if(collection1!=null) {
										coursesPlayback.setIsCollection(1);
									}
									RecentlyStudiedQo recentlyStudiedQo=new RecentlyStudiedQo();
									recentlyStudiedQo.setSeachCourses(true);
									recentlyStudiedQo.setSeachTeacher(true);
									recentlyStudiedQo.setSeachUser(true);
									recentlyStudiedQo.setCoursesId(Long.parseLong(coursesPlaybackId));
									recentlyStudiedQo.setUserId(user.getId());
									RecentlyStudied recentlyStudied1= recentlyStudiedService.queryUnique(recentlyStudiedQo);
									if(recentlyStudied1!=null) {
										recentlyStudied1.setRecentLearnTime(new Date());
										recentlyStudiedService.update(recentlyStudied1);
									}else {
										//最近观看
										//										RecentlyStudied recentlyStudied=new RecentlyStudied();
										//										recentlyStudied.setTeacher(courses.getUploader());
										//										recentlyStudied.setUser(user);
										//										recentlyStudied.setRecentLearnTime(new Date());
										//										recentlyStudied.setCourses(courses);
										//										recentlyStudiedService.save(recentlyStudied);
									}

								}
							}
						}

						if(coursesPlayback.getIsPay()!=null&&coursesPlayback.getIsPay()==1) {
							SysProperties sysProperties=SysProperties.getInstance();
							String imagePath=sysProperties.get("imagePath");
							if(coursesPlayback.getUploader()!=null&&coursesPlayback.getUploader().getAvatar()!=null){
								coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
							}
							resMap.put("code", "200");


							coursesPlayback.setVideo(imagePath+"/videofile/"+coursesPlayback.getVideo());
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							resMap.put("message", coursesPlayback);
							return JSONObject.fromObject(resMap).toString();
						}else {
							resMap.put("code", "200");
							coursesPlayback.setVideo("");
							resMap.put("message", coursesPlayback);
							return JSONObject.fromObject(resMap).toString();
						}
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
				
			}
		}
	}

	/**
	 * 增加人气值
	 * @param typeOne
	 * @param typeTwo
	 * @return
	 */
	//	@ResponseBody
	//	@RequestMapping(value = "/lookCourses")
	//	public String lookCourses( @RequestParam(value="coursesId", required=true) String coursesId) {
	//	    Map<String, Object> resMap=new HashMap<String, Object>();
	//	    CoursesQo qo=new CoursesQo();
	//	    qo.setCoursesId(Long.parseLong(coursesId));
	//	    Courses courses=coursesService.queryUnique(qo);
	//	    if(courses!=null) {
	//	    	courses.setRqz(courses.getRqz()+1);
	//	    }
	//	    coursesService.update(courses);
	//        resMap.put("code", "200");
	//        resMap.put("message", "成功");
	//        return JSONObject.fromObject(resMap).toString();
	//	}
	//	
	/**
	 * 二级课程列表
	 * @param typeOne
	 * @param typeTwo
	 * @return
	 */
//	@ResponseBody
//	@RequestMapping(value = "/coursesPlayBackTypepage")
//	public String coursesPlayBackTypepage( @RequestParam(value="typeOne", required=true) String typeOne,String typeTwo,@RequestParam(value="token", required=false) String token) {
//		Map<String, Object> resMap=new HashMap<String, Object>();
//		AdminSessionQo adminSessionQo=new AdminSessionQo();
//		adminSessionQo.setToken(token);
//		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
//		if(adminSession==null) {
//			resMap.put("code", "406");
//			resMap.put("message", "不存在此账号！");
//			return JSONObject.fromObject(resMap).toString();
//		}else {
//			UserQo userQo=new UserQo();
//			userQo.setUserName(adminSession.getLoginName());
//			User user= userService.queryUnique(userQo);
//			if(user==null) {
//				resMap.put("code", "406");
//				resMap.put("message", "未登录!");
//				return JSONObject.fromObject(resMap).toString();
//			}else {
//				if(user.getIsRole()==2) {
//					StudentQo stuqo= new StudentQo();
//					stuqo.setUserid(user.getId());
//					Student student=studentService.queryUnique(stuqo);
//					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
//					hasStudentQo.setStudentId(student.getId());
//					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
//					if(hasStudent!=null) {
//						CoursesPlaybackQo qo=new CoursesPlaybackQo();
//						qo.setCourseTypeOne(Long.parseLong(typeOne));
//						qo.setCourseTypeTwo(Long.parseLong(typeTwo));
//						qo.setOrganizationId(hasStudent.getOrganization().getId());
//						qo.setSeachTeacher(true);
//						Pagination pagination = new Pagination();
//						pagination.setPageSize(8);
//						pagination.setCondition(qo);
//						pagination = coursesPlaybackService.queryPagination(pagination);
//						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
//						SysProperties sysProperties=SysProperties.getInstance();
//						String imagePath=sysProperties.get("imagePath");
//						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
//							TeachersQo teachersQo=new TeachersQo();
//							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
//							teachersQo.setSeachUser(true);
//							Teacher teacher=teacherService.queryUnique(teachersQo);
//							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
//
//								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
//								}else {
//									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
//								}
//							}
//							coursesPlayback.setUserId(teacher.getUser().getId());
//							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
//							coursesPlayback.setVideo("");
//							coursesPlayback.setCourseintroduction("");
//							coursesPlayback.setCourseintroduction("");
//							coursesPlayback.setCourseTypeOneId(null);
//							coursesPlayback.setCourseTypeTwoId(null);
//							//			courses.set
//						}
//
//						resMap.put("code", "200");
//						resMap.put("message", coursesPlaybackList);
//						return JSONObject.fromObject(resMap).toString();
//					}else {
//						resMap.put("code", "404");
//						resMap.put("message", "你没有所属于的机构！");
//						return JSONObject.fromObject(resMap).toString();
//					}
//
//				}else {
//					TeachersQo teachersQo1=new TeachersQo();
//					teachersQo1.setSeachOrganization(true);
//					teachersQo1.setSeachUser(true);
//					teachersQo1.setUserId(user.getId());
//					Teacher t2=teacherService.queryUnique(teachersQo1);
//					if(t2!=null) {
//						CoursesPlaybackQo qo=new CoursesPlaybackQo();
//						qo.setCourseTypeOne(Long.parseLong(typeOne));
//						qo.setCourseTypeTwo(Long.parseLong(typeTwo));
//						qo.setOrganizationId(t2.getTraininginstitution().getId());
//						qo.setSeachTeacher(true);
//						Pagination pagination = new Pagination();
//						pagination.setPageSize(8);
//						pagination.setCondition(qo);
//						pagination = coursesPlaybackService.queryPagination(pagination);
//						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
//						SysProperties sysProperties=SysProperties.getInstance();
//						String imagePath=sysProperties.get("imagePath");
//						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
//							TeachersQo teachersQo=new TeachersQo();
//							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
//							teachersQo.setSeachUser(true);
//							Teacher teacher=teacherService.queryUnique(teachersQo);
//							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
//
//								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {
//								}else {
//									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
//								}
//							}
//							coursesPlayback.setUserId(teacher.getUser().getId());
//							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
//							coursesPlayback.setVideo("");
//							coursesPlayback.setCourseintroduction("");
//							coursesPlayback.setCourseintroduction("");
//							coursesPlayback.setCourseTypeOneId(null);
//							coursesPlayback.setCourseTypeTwoId(null);
//							//			courses.set
//						}
//
//						resMap.put("code", "200");
//						resMap.put("message", coursesPlaybackList);
//						return JSONObject.fromObject(resMap).toString();
//					}else {
//						resMap.put("code", "404");
//						resMap.put("message", "你没有所属于的机构！");
//						return JSONObject.fromObject(resMap).toString();
//					}
//					
//				}
//				
//			}
//		}
//
//
//	}

	/**
	 * 二级课程列表分页
	 * @param typeOne
	 * @param typeTwo
	 * @param page
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesPlayBackType")
	public String coursesPlayBackType( @RequestParam(value="typeOne", required=true) String typeOne,String typeTwo,@RequestParam(value = "page", defaultValue = "1")Integer page,@RequestParam(value="token", required=false) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo stuqo= new StudentQo();
					stuqo.setSeachStudent(true);
					stuqo.setUserid(user.getId());
					Student student=studentService.queryUnique(stuqo);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCourseTypeOne(Long.parseLong(typeOne));
						qo.setSeachTeacher(true);
						qo.setOrganizationId(hasStudent.getOrganization().getId());
						if(StringUtils.isNotEmpty(typeTwo)&&!"null".equals(typeTwo)) {
							qo.setCourseTypeTwo(Long.parseLong(typeTwo));
						}
						Pagination pagination = new Pagination();
						pagination.setPageSize(10);
						pagination.setCondition(qo);
						pagination.setPageNo(page);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							TeachersQo teachersQo=new TeachersQo();
							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
							teachersQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teachersQo);
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setUserId(teacher.getUser().getId());
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo teaQo=new TeachersQo();
					teaQo.setSeachOrganization(true);
					teaQo.setSeachUser(true);
					teaQo.setUserId(user.getId());
					Teacher tea=teacherService.queryUnique(teaQo);
					if(tea!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCourseTypeOne(Long.parseLong(typeOne));
						qo.setSeachTeacher(true);
						qo.setOrganizationId(tea.getTraininginstitution().getId());
						if(StringUtils.isNotEmpty(typeTwo)&&!"null".equals(typeTwo)) {
							qo.setCourseTypeTwo(Long.parseLong(typeTwo));
						}
						Pagination pagination = new Pagination();
						pagination.setPageSize(10);
						pagination.setCondition(qo);
						pagination.setPageNo(page);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							TeachersQo teachersQo=new TeachersQo();
							teachersQo.setTeacherId(coursesPlayback.getUploader().getId());
							teachersQo.setSeachUser(true);
							Teacher teacher=teacherService.queryUnique(teachersQo);
							if(StringUtils.isNotEmpty(coursesPlayback.getUploader().getAvatar())) {
								if(coursesPlayback.getUploader().getAvatar().indexOf(imagePath+"/picture/")>=0) {

								}else {
									coursesPlayback.getUploader().setAvatar(imagePath+"/picture/"+coursesPlayback.getUploader().getAvatar());
								}
							}
							coursesPlayback.setUserId(teacher.getUser().getId());
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}

						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
					
				}
				
			}
		}
	}
	/**
	 * 一级课程列表
	 * @param typeOne
	 * @param page
	 * @param typeTwo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/coursesPlayBackTypeOnepage")
	public String coursesPlayBackTypeOnepage( @RequestParam(value="typeOne", required=true) String typeOne,@RequestParam(value = "page", defaultValue = "1")Integer page,String typeTwo,@RequestParam(value="token", required=false) String token) {
		Map<String, Object> resMap=new HashMap<String, Object>();
		AdminSessionQo adminSessionQo=new AdminSessionQo();
		adminSessionQo.setToken(token);
		AdminSession adminSession=adminSessionService.queryUnique(adminSessionQo);
		if(adminSession==null) {
			resMap.put("code", "406");
			resMap.put("message", "不存在此账号！");
			return JSONObject.fromObject(resMap).toString();
		}else {
			UserQo userQo=new UserQo();
			userQo.setUserName(adminSession.getLoginName());
			User user= userService.queryUnique(userQo);
			if(user==null) {
				resMap.put("code", "406");
				resMap.put("message", "未登录!");
				return JSONObject.fromObject(resMap).toString();
			}else {
				if(user.getIsRole()==2) {
					StudentQo stuqo= new StudentQo();
					stuqo.setSeachStudent(true);
					stuqo.setUserid(user.getId());
					Student student=studentService.queryUnique(stuqo);
					OrganizationHasStudentQo hasStudentQo=new OrganizationHasStudentQo();
					hasStudentQo.setStudentId(student.getId());
					OrganizationHasStudent hasStudent= osdService.queryUnique(hasStudentQo);
					if(hasStudent!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCourseTypeOne(Long.parseLong(typeOne));
						if(StringUtils.isNotEmpty(typeTwo)){
							qo.setCourseTypeTwo(Long.parseLong(typeTwo));
						}
						
						qo.setOrganizationId(hasStudent.getOrganization().getId());
						Pagination pagination = new Pagination();
						pagination.setPageSize(10);
						pagination.setPageNo(page);
						pagination.setCondition(qo);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}
						pagination.setList(coursesPlaybackList);
						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}else {
					TeachersQo teacheqo=new TeachersQo();
					teacheqo.setUserId(user.getId());
					teacheqo.setSeachOrganization(true);
					teacheqo.setSeachUser(true);
					Teacher teach=teacherService.queryUnique(teacheqo);
					if(teach!=null) {
						CoursesPlaybackQo qo=new CoursesPlaybackQo();
						qo.setCourseTypeOne(Long.parseLong(typeOne));
						if(StringUtils.isNotEmpty(typeTwo)){
							qo.setCourseTypeTwo(Long.parseLong(typeTwo));
						}
						qo.setOrganizationId(teach.getTraininginstitution().getId());
						Pagination pagination = new Pagination();
						pagination.setPageSize(10);
						pagination.setPageNo(page);
						pagination.setCondition(qo);
						pagination = coursesPlaybackService.queryPagination(pagination);
						List<CoursesPlayback> coursesPlaybackList=(List<CoursesPlayback>)pagination.getList();
						SysProperties sysProperties=SysProperties.getInstance();
						String imagePath=sysProperties.get("imagePath");
						for(CoursesPlayback coursesPlayback:coursesPlaybackList) {
							coursesPlayback.setImge(imagePath+"/img/"+coursesPlayback.getImge());
							coursesPlayback.setVideo("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseintroduction("");
							coursesPlayback.setCourseTypeOneId(null);
							coursesPlayback.setCourseTypeTwoId(null);
							//			courses.set
						}
						pagination.setList(coursesPlaybackList);
						resMap.put("code", "200");
						resMap.put("message", pagination);
						return JSONObject.fromObject(resMap).toString();
					}else {
						resMap.put("code", "404");
						resMap.put("message", "你没有所属于的机构！");
						return JSONObject.fromObject(resMap).toString();
					}
				}
				
			}

		}
		
	}

}

